import { STORAGE_TOKEN_KEY } from '@/types/meta';
import { Role } from '@/types/role';

import { jwtDecode, JwtPayload } from 'jwt-decode';

const useAuthority = () => {
	const token = ref<string>();

	const role = computed(() => token.value && jwtDecode<JwtPayload>(token.value)['_doc']['role']);

	onMounted(() => {
		token.value = localStorage.getItem(STORAGE_TOKEN_KEY) + '';
	});

	const hasAuthorith = (needRole: Role) => {
		return needRole === role.value;
	};
	return {
		role,
		hasAuthorith,
	};
};

export default useAuthority;
